﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using cfg = System.Configuration;
using GoldPrinter;
namespace BYManageInfoSystem
{
    public partial class Frm_FinBuyGoodsContract : Form
    {
        private readonly DGBY.BLL.BGCKHTTab m_bllBGCKHTTab = new DGBY.BLL.BGCKHTTab();
        private DGBY.BLL.CargoBoxTab m_bllCargoBoxTab = new DGBY.BLL.CargoBoxTab();
        private DGBY.BLL.PaidanRegisterTab m_bllPaidanRegisterTab = new DGBY.BLL.PaidanRegisterTab();
      //  private DGBY.BLL.BGDComeBackTab m_bllBGDComeBackTab = new DGBY.BLL.BGDComeBackTab();
        protected string templetFilePath = cfg.ConfigurationSettings.AppSettings["TempletFilePath"];
        public Frm_FinBuyGoodsContract()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int nId = Convert.ToInt32(this.comboBoxContractNo.SelectedValue.ToString(), 10);
            DataSet ds = m_bllBGCKHTTab.GetFinBuyGoodsContractList("PaidanRegisterTab.PaidanRegisterTabID = '" + nId.ToString() + "'");

            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {


            }
            else
            {
                MessageBox.Show("该合同号对应的回收报关单未回来!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            DataSet ds2 = m_bllBGCKHTTab.GetFinBuyGoodsContractDetailList("PaidanRegisterTab.PaidanRegisterTabID = '" + nId.ToString() + "'");

            if (ds2.Tables.Count > 0 && ds2.Tables[0].Rows.Count > 0)
            {


            }
            else
            {
                MessageBox.Show("该合同号对应的回收报关单没有明细!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;

            }
            string strGuiNoLst = "";
            DataSet ds3 = m_bllBGCKHTTab.GetAllCargoBoxInfoEx("PaidanRegisterTab.PaidanRegisterTabID = '" + nId.ToString() + "'");
            if (ds3.Tables.Count > 0 && ds3.Tables[0].Rows.Count > 0)
            {
                string str = ds3.Tables[0].Rows[0]["集装箱柜号"].ToString();
                string[] arr = str.Split(',');
                string tempa = "";
                int n = 0;
                foreach (string ch in arr)
                {

                    if (ch != "")
                    {
                        if (n == 0)
                            tempa += ch;
                        else
                            tempa += "\n" + ch;
                    }

                    n++;
                }
                strGuiNoLst = tempa;

            }
            DataTable dt = (DataTable)ds2.Tables[0];
            DataTable dt1 = (DataTable)ds.Tables[0];
            //DataSet DsBYCompInfo = bllBYCompanyTab.GetList("CompanyName = '" + dt1.Rows[0]["卖方"].ToString() + "'");
            //DataSet DsSHCompInfo = m_bllSHCompTab.GetList("CompanyName = '" + dt1.Rows[0]["买方"].ToString() + "'");
            if (dt1 != null)
            {

            }
            else
            {
                MessageBox.Show("购货合同记录不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (dt != null)
            {
                if (dt.Rows.Count > 0)
                {
                    SaveFileDialog sfd = new SaveFileDialog();
                    sfd.Filter = "Excel文件(*.xls)|*.xls";
                    string outFile = "";

                    if (sfd.ShowDialog() == DialogResult.OK)
                    {
                        outFile = sfd.FileName;
                    }
                    else
                    {
                        return;
                    }

                    string templetFile = templetFilePath + "财务_购货合同.xlt";

                    ExcelBase eB = new ExcelBase(templetFile, outFile);

                    try
                    {
                        eB.FinBuyGoodsConstract_DataTableToExcel(dt, dt1, 16, 8, 1, "页", 1, strGuiNoLst, "", false);
                    }
                    catch (System.Exception ex)
                    {

                    }

                    eB.Close();
                }
            }
            else
            {
                MessageBox.Show("购货合同明细不存在!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }

        private void Frm_FinBuyGoodsContract_Load(object sender, EventArgs e)
        {
            DataSet ds = m_bllPaidanRegisterTab.GetAllPaiDanList("");
            if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
            {
                this.comboBoxContractNo.DataSource = ds.Tables[0];
                this.comboBoxContractNo.ValueMember = "PaidanRegisterTabID";
                this.comboBoxContractNo.DisplayMember = "合同编号";
                this.comboBoxContractNo.AutoCompleteSource = AutoCompleteSource.ListItems;
                this.comboBoxContractNo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                this.comboBoxContractNo.SelectedIndex = 0;
            }
        }
    }
}
